#pragma once

#include "ui_render.h"
#include "ui_offset_dc.h"

_SSL_BEGIN
_SSUIL_BEGIN


#pragma warning(push)
#pragma warning(disable:4251)
#pragma warning(disable:4250)
#pragma warning(disable:4275)

class SSUIRENDER_CLASS UIBackgroundCanvas :
	public IUIWindowBgCanvasInterface,
	virtual public ref_counted_base
{
public:
	UIBackgroundCanvas();
	virtual ~UIBackgroundCanvas();

	// IUIWindowBgCanvasInterface
public:
	virtual bool Init(HWND hwnd);
	virtual bool Init(HWND hwnd, const RECT &rt);
	virtual void WindowResized();
	virtual bool Uninit();
	virtual void SetTransparentColor(unsigned long color);
	virtual void SetBackgroundImage(const UIPresetImage &bg_img);
	virtual void SetAlphaMode(bool translucent, const RECT &alpha_border);

	virtual void* BeginRender(const UIRenderStruct &params);
	virtual void EndRender(void *dc);

	virtual int GetWidth();
	virtual int GetHeight();

	virtual scoped_ref_ptr<IUIWindowBgCanvasInterface> New();

	//
protected:
	void* InternalBeginRenderOnMessageDCT(IUIResourceRenderInterface *rr, UINT msg, WPARAM wparam, LPARAM lparam);
	void InternalEndRender(void *dc);
	void* InternalBeginRenderForOnlyAnimation(const RECT &invalidte_rt);

protected:
	void DealOnAlphaArea(byte *pixel_begin, int bytes_per_pixel);
	void DealOnNoAlphaArea(byte *pixel_begin, int bytes_per_pixel);
	void DealOnLayeredWindow();

protected:
	HWND host_window_;
	unsigned long transparent_color_;
	HBRUSH transparent_color_brush_;
	HDC device_dc_;
	enum UIRenderStep
	{
		kRSNone,
		kRSPaintStructControlsLayer,
		kRSPaintStructFinalLayer,
		kRSDCControlsLayer,
		kRSDCFinalLayer,
		kRSDCAnimationFinalLayer,
	};
	UIRenderStep rstype_;
	bool translucent_;
	RECT alpha_borders_;

	// offscreen_controls_dc_ = offscreen_bg_dc_ + control's draws
	// offscreen_final_dc_ = offscreen_controls_dc_ + animations's draws
	UIOffScreenDC offscreen_final_dc_;
	UIOffScreenDC offscreen_bg_dc_;
	UIOffScreenDC offscreen_controls_dc_;
	UIOffScreenDC xp_offscreen_dc_;
	int alpha_value_;

	UIRenderDrawContextType dctype_;
	PAINTSTRUCT ps_;

	UIPresetImage bg_img_;
	bool bg_img_update_;

};
#pragma warning(pop)

_SSUIL_END
_SSL_END

